<?php
/**
 * Author: Traveler.
 * Time  : 2021/8/31 16:53
 */

namespace app\middleware;


use app\Request;
use Closure;
use think\Middleware;
use think\Response;

/**
 * Token验证中间件类
 *
 * @package app\middleware
 */
class Auth extends Middleware
{
    /**
     * @param Request $request
     * @param Closure $next
     * @return Response
     */
    public function handle(Request $request, Closure $next): Response
    {
        if ($request->isOptions()) {
            return response();
        }

        $authorization = $request->header('Authorization') ?? '';
        $payload       = verify_token($authorization);

        // 向 request 注入自定义参数，方便获取 token payload 中的参数
        $request->tokenPayload = $payload;

        return $next($request);
    }
}